var $;
layui.config({
	base : "js/"
}).use(['form','layer','jquery'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage;
		$ = layui.jquery;

    //初始化加载供货商名称
    getSupplierList();
    
    //供货商选择监听
    form.on("select(supplierName)",function(data)
    {
    	$(".waterName").empty();
    	var supplierId = $(data.elem).val();
    	alert(supplierId);
    	var dto = {"supplierId":supplierId};
	      $.ajax({
	      	    type: "POST",
	      	    dataType: "json",
	      	    url : API_URL_HEAD+"water/select",
	      	    contentType: "application/json; charset=utf-8",
	      	    async:true,
	      	    data:JSON.stringify(dto),
	      	  	success: function (data) {  
	      	  		if(data.code == "0"){
		      	  		var jsArr = data.data;
		      	  		var apartAreaObj = $(".waterName");
		      	  		createSelectOptions(apartAreaObj,jsArr,"waterName");
		      	  		form.render(null,"waterName");
	      	  		}
	      	    },
	      	    error: function(data) { 
	      	    	console.log(data);
	      	    	layer.alert("服务器忙，请稍后重试");
	      	    }
	      	});	
    	//根据选中的供应商ID获取水下拉列表
    });
    
    
    //水票类型选择监听
    form.on("select(ticketType)",function(data)
    {
    	var ticketType = $(".ticketType").val();
    	if(ticketType == 1)
    	{
    		$(".relationId").empty();
    		$(".relationId").parent().parent().hide();
    	}else
    	{
    		getWaterTicketsList();
    		$(".relationId").parent().parent().show();
    	}
    });
    
 	form.on("submit(addUser)",function(data){
 		//是否添加过信息
	 	var dto = {
 			"ticketName": $(".ticketName").val(),
 			"ticketType": $(".ticketType").val(),
 			"isSwap":data.field.isSwap,
 			"waterId":$(".waterName").val(),
 			"ticketCostPrice": $(".ticketCostPrice").val(),
 			"ticketMarketPrice": $(".ticketMarketPrice").val(),
 			"relationId":$(".relationId").val(),
 			"supplierId":$(".supplierName").val(),
 			"remark": $(".remark").val()
 		};
        $.ajax({
	        url: API_URL_HEAD+"waterTicket/create",
	        type: 'post',
	        contentType: "application/json; charset=utf-8",
	        data: JSON.stringify(dto),
	        dataType: 'json',
	        success: function (data) {
	            if(data.code == 0){
	               //刷新父页面
		 			parent.location.reload();
	            }
	        }
    	});
 		return false;
 	})
 	/*
	 *供应商列表
	 */
	function getSupplierList(){
		var dto = {};
	      $.ajax({
	      	    type: "POST",
	      	    dataType: "json",
	      	    url : API_URL_HEAD+"supplier/select",
	      	    contentType: "application/json; charset=utf-8",
	      	    async:false,
	      	    data:JSON.stringify(dto),
	      	  	success: function (data) {  
	      	  		if(data.code == "0"){
		      	  		var jsArr = data.data;
		      	  		var apartAreaObj = $(".supplierName");
		      	  		createSelectOptions(apartAreaObj,jsArr,"supplierName");
		      	  		form.render(null,"supplierName");
	      	  		}
	      	    },
	      	    error: function(data) { 
	      	    	console.log(data);
	      	    	layer.alert("服务器忙，请稍后重试");
	      	    }
	      	});	
	}
	/*
	 *水列表
	 */
	function getWaterTicketsList(){
		var dto = {
			"ticketType":1,
			"isSwap":1
		};
	      $.ajax({
	      	    type: "POST",
	      	    dataType: "json",
	      	    url : API_URL_HEAD+"waterTicket/select",
	      	    contentType: "application/json; charset=utf-8",
	      	    async:false,
	      	    data:JSON.stringify(dto),
	      	  	success: function (data) {  
	      	  		if(data.code == "0"){
		      	  		var jsArr = data.data;
		      	  		var apartAreaObj = $(".relationId");
		      	  		createSelectOptions(apartAreaObj,jsArr,"ticketName");
		      	  		form.render(null,"relationId");
	      	  		}
	      	    },
	      	    error: function(data) { 
	      	    	console.log(data);
	      	    	layer.alert("服务器忙，请稍后重试");
	      	    }
	      	});	
	}
	//生成select options
	function createSelectOptions(selObj,dataArray,name,id){
		for(var i in dataArray){
			var every = dataArray[i];
			if(id==undefined){
				$(selObj).append("<option value='"+every["id"]+"'>"+every[name]+"</option>");
			}else{
				if(every["id"]==id){
					$(selObj).append("<option value='"+every["id"]+"'  selected=\"selected\">"+every[name]+"</option>");
				}else{
					$(selObj).append("<option value='"+every["id"]+"'>"+every[name]+"</option>");
				}
			}
			
		}
	}
	
})

function ticketTypeChange()
{
	alert(0000000);
}
//格式化时间
function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}
